package com.kkb.controller;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.kkb.exception.TeamIdException;
import com.kkb.exception.TeamNameException;
import com.kkb.service.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-09-12 10:55
 * @Modified By:
 */
@Controller
public class TeamController {

    @Autowired
    @Qualifier(value = "teamService")
    private TeamService teamService;

    @RequestMapping("/hello.do")
    public ModelAndView add(){
        System.out.println("TeamController-----add-----");
        teamService.add();
        ModelAndView view = new ModelAndView();
        view.addObject("teamName","湖人");
        view.setViewName("index");
        return view;
    }

    @RequestMapping("/redict1")
    public String redict1(HttpServletRequest req){
        req.setAttribute("username","张三");
        return "redirect:/redict.jsp";
    }

    @RequestMapping("/redict2")
    public ModelAndView redict2(ModelAndView modelAndView){
        modelAndView.addObject("username","詹桑");
        modelAndView.setViewName("redirect:/redict.jsp");
        return modelAndView;
    }

    @RequestMapping("/exception1/{id}/{name}")
    public ModelAndView exception(@PathVariable("id") Integer teamId,
                                  @PathVariable("name") String teamName) throws TeamIdException, TeamNameException {
        ModelAndView mv = new ModelAndView();
        if (teamId>1000){
            throw new TeamIdException("球队id出错");
        }
        if ("test".equals(teamName)){
            throw new TeamNameException("球队名称出错");
        }
        int a=10/0;
        mv.setViewName("ok");
        return mv;
    }

    /*@ExceptionHandler(value = {TeamIdException.class,TeamNameException.class,Exception.class})
    public ModelAndView exception2(Exception ex){

        ModelAndView mv = new ModelAndView();
        if (ex instanceof TeamIdException){
            mv.setViewName("error1");
        } else if (ex instanceof TeamNameException){
            mv.setViewName("error2");
        }else{
            mv.setViewName("error3");
        }
        return mv;
    }*/
}
